<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="盘点单号" prop="checkNumber">
        <el-input
          v-model="queryParams.checkNumber"
          placeholder="请输入盘点单号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="盘点主题" prop="checkTheme">
        <el-input
          v-model="queryParams.checkTheme"
          placeholder="请输入盘点主题"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="盘点部门" prop="checkDepmId">
        <el-input
          v-model="queryParams.checkDepmId"
          placeholder="请输入盘点部门"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="盘点人" prop="checkUserId">
        <el-input
          v-model="queryParams.checkUserId"
          placeholder="请输入盘点人"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="盘点日期" prop="checkDate">
        <el-date-picker clearable
          v-model="queryParams.checkDate"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择盘点日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="盘点开始时间" prop="checkStartDate">
        <el-date-picker clearable
          v-model="queryParams.checkStartDate"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择盘点开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="盘点结束日期" prop="checkEndDate">
        <el-date-picker clearable
          v-model="queryParams.checkEndDate"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择盘点结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="档案列表索引集" prop="detailDataIndex">
        <el-input
          v-model="queryParams.detailDataIndex"
          placeholder="请输入档案列表索引集"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="0-正常,1-删除" prop="delflag">
        <el-input
          v-model="queryParams.delflag"
          placeholder="请输入0-正常,1-删除"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['archive:check:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['archive:check:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['archive:check:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['archive:check:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="checkList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键ID" align="center" prop="checkId" />
      <el-table-column label="盘点单号" align="center" prop="checkNumber" />
      <el-table-column label="盘点主题" align="center" prop="checkTheme" />
      <el-table-column label="盘点部门" align="center" prop="checkDepmId" />
      <el-table-column label="盘点人" align="center" prop="checkUserId" />
      <el-table-column label="盘点日期" align="center" prop="checkDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.checkDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="盘点开始时间" align="center" prop="checkStartDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.checkStartDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="盘点结束日期" align="center" prop="checkEndDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.checkEndDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="档案列表索引集" align="center" prop="detailDataIndex" />
      <el-table-column label="0-正常,1-删除" align="center" prop="delflag" />
      <el-table-column label="状态 0-录入 1-盘点中 2-盘点超期 3-盘点结束" align="center" prop="status" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="判断类型1-仅档案盒2-仅档案3-全部" align="center" prop="checkType" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            type="text"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['archive:check:edit']"
          >修改</el-button>
          <el-button
            type="text"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['archive:check:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改盘点对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="checkRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="盘点单号" prop="checkNumber">
          <el-input v-model="form.checkNumber" placeholder="请输入盘点单号" />
        </el-form-item>
        <el-form-item label="盘点主题" prop="checkTheme">
          <el-input v-model="form.checkTheme" placeholder="请输入盘点主题" />
        </el-form-item>
        <el-form-item label="盘点部门" prop="checkDepmId">
          <el-input v-model="form.checkDepmId" placeholder="请输入盘点部门" />
        </el-form-item>
        <el-form-item label="盘点人" prop="checkUserId">
          <el-input v-model="form.checkUserId" placeholder="请输入盘点人" />
        </el-form-item>
        <el-form-item label="盘点日期" prop="checkDate">
          <el-date-picker clearable
            v-model="form.checkDate"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择盘点日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="盘点开始时间" prop="checkStartDate">
          <el-date-picker clearable
            v-model="form.checkStartDate"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择盘点开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="盘点结束日期" prop="checkEndDate">
          <el-date-picker clearable
            v-model="form.checkEndDate"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择盘点结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="档案列表索引集" prop="detailDataIndex">
          <el-input v-model="form.detailDataIndex" placeholder="请输入档案列表索引集" />
        </el-form-item>
        <el-form-item label="0-正常,1-删除" prop="delflag">
          <el-input v-model="form.delflag" placeholder="请输入0-正常,1-删除" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-divider content-position="center">资产盘点子信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="Plus" @click="handleAddArcCheckDetails">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="Delete" @click="handleDeleteArcCheckDetails">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="arcCheckDetailsList" :row-class-name="rowArcCheckDetailsIndex" @selection-change="handleArcCheckDetailsSelectionChange" ref="arcCheckDetails">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="档案盒ID" prop="boxId" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.boxId" placeholder="请输入档案盒ID" />
            </template>
          </el-table-column>
          <el-table-column label="盘点结果 0正常 1盘亏2盘盈" prop="checkResult" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.checkResult" placeholder="请输入盘点结果 0正常 1盘亏2盘盈" />
            </template>
          </el-table-column>
          <el-table-column label="档案ID" prop="archiveId" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.archiveId" placeholder="请输入档案ID" />
            </template>
          </el-table-column>
          <el-table-column label="盘点时间" prop="checkDate" width="240">
            <template #default="scope">
              <el-date-picker clearable
                v-model="scope.row.checkDate"
                type="date"
                value-format="YYYY-MM-DD"
                placeholder="请选择盘点时间">
              </el-date-picker>
            </template>
          </el-table-column>
          <el-table-column label="删除标识" prop="delflag" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.delflag" placeholder="请输入删除标识" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Check">
import { listCheck, getCheck, delCheck, addCheck, updateCheck } from "@/api/archive/check";

const { proxy } = getCurrentInstance();

const checkList = ref([]);
const arcCheckDetailsList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const checkedArcCheckDetails = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    checkNumber: null,
    checkTheme: null,
    checkDepmId: null,
    checkUserId: null,
    checkDate: null,
    checkStartDate: null,
    checkEndDate: null,
    detailDataIndex: null,
    delflag: null,
    status: null,
    checkType: null
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询盘点列表 */
function getList() {
  loading.value = true;
  listCheck(queryParams.value).then(response => {
    checkList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    checkId: null,
    checkNumber: null,
    checkTheme: null,
    checkDepmId: null,
    checkUserId: null,
    checkDate: null,
    checkStartDate: null,
    checkEndDate: null,
    detailDataIndex: null,
    delflag: null,
    status: "0",
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null,
    checkType: null
  };
  arcCheckDetailsList.value = [];
  proxy.resetForm("checkRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.checkId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加盘点";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const checkId = row.checkId || ids.value
  getCheck(checkId).then(response => {
    form.value = response.data;
    arcCheckDetailsList.value = response.data.arcCheckDetailsList;
    open.value = true;
    title.value = "修改盘点";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["checkRef"].validate(valid => {
    if (valid) {
      form.value.arcCheckDetailsList = arcCheckDetailsList.value;
      if (form.value.checkId != null) {
        updateCheck(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addCheck(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const checkIds = row.checkId || ids.value;
  proxy.$modal.confirm('是否确认删除盘点编号为"' + checkIds + '"的数据项？').then(function() {
    return delCheck(checkIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 资产盘点子序号 */
function rowArcCheckDetailsIndex({ row, rowIndex }) {
  row.index = rowIndex + 1;
}

/** 资产盘点子添加按钮操作 */
function handleAddArcCheckDetails() {
  let obj = {};
  obj.boxId = "";
  obj.checkResult = "";
  obj.archiveId = "";
  obj.checkDate = "";
  obj.delflag = "";
  obj.remark = "";
  arcCheckDetailsList.value.push(obj);
}

/** 资产盘点子删除按钮操作 */
function handleDeleteArcCheckDetails() {
  if (checkedArcCheckDetails.value.length == 0) {
    proxy.$modal.msgError("请先选择要删除的资产盘点子数据");
  } else {
    const arcCheckDetailss = arcCheckDetailsList.value;
    const checkedArcCheckDetailss = checkedArcCheckDetails.value;
    arcCheckDetailsList.value = arcCheckDetailss.filter(function(item) {
      return checkedArcCheckDetailss.indexOf(item.index) == -1
    });
  }
}

/** 复选框选中数据 */
function handleArcCheckDetailsSelectionChange(selection) {
  checkedArcCheckDetails.value = selection.map(item => item.index)
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('archive/check/export', {
    ...queryParams.value
  }, `check_${new Date().getTime()}.xlsx`)
}

getList();
</script>
